package com.jianchi.fsp.appui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by fsp on 16-5-21.
 */
public class HistoryAdapter extends BaseAdapter {

    List<History> histories;
    Context context;
    SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public HistoryAdapter(List<History> _hm, Context _c){
        this.histories=_hm;
        this.context=_c;
    }

    @Override
    public int getCount() {
        return histories.size();
    }

    @Override
    public Object getItem(int i) {
        return histories.get(histories.size()-i-1);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        History holder= histories.get(histories.size()-i-1);
        if(null==convertView)
        {
            convertView= LayoutInflater.from(context).inflate(R.layout.search_item, null);
        }

        ImageView imageSearchItem=(ImageView)convertView.findViewById(R.id.imageSearchItem);
        TextView textViewAppName = (TextView)convertView.findViewById(R.id.textViewAppName);
        TextView textViewTime = (TextView)convertView.findViewById(R.id.textViewTime);
        textViewAppName.setText(holder.appName);
        textViewTime.setText(fmt.format(holder.viewTime));
        Picasso.with(imageSearchItem.getContext())
                .load(holder.picUrl)
                .resize(dip2px(72), dip2px(72))
                .into(imageSearchItem);
        convertView.setTag(holder);
        return convertView;
    }

    int dip2px(float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
